#include "QGraphicsView.h"
#include <QGraphicsScene>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QPainter>
#include <cmath>

CADView::CADView(QWidget *parent)
        : QGraphicsView(new QGraphicsScene(), parent) {
    setRenderHint(QPainter::Antialiasing);
    setDragMode(QGraphicsView::ScrollHandDrag);
    scene()->setSceneRect(-10000, -10000, 20000, 20000);



}

CADView::~CADView() {
    // 清理资源
    delete horizontalLine;
    delete verticalLine;
    delete movingCircle;
}

void CADView::wheelEvent(QWheelEvent *event) {
    const double scaleFactor = 1.15;
    if (event->angleDelta().y() > 0) {
        scale(scaleFactor, scaleFactor);  // 放大
    } else {
        scale(1.0 / scaleFactor, 1.0 / scaleFactor);  // 缩小
    }
    update();  // 更新视图，重绘背景
}

void CADView::drawBackground(QPainter *painter, const QRectF &rect) {
    QRectF sceneRect = this->sceneRect();
    QTransform currentTransform = painter->transform();
    qreal currentScale = currentTransform.m11();
    int gridSize = static_cast<int>(baseGridSize / currentScale); // 当前缩放下的网格大小
    if (gridSize < 5) {
        gridSize = 5;
    }

    painter->setPen(QPen(QColor(200, 200, 200), 0.5 / currentScale)); // 动态线宽

    // 计算并绘制网格线
    for (qreal x = std::floor(sceneRect.left() / gridSize) * gridSize; x < sceneRect.right(); x += gridSize) {
        painter->drawLine(QLineF(x, sceneRect.top(), x, sceneRect.bottom()));
    }
    for (qreal y = std::floor(sceneRect.top() / gridSize) * gridSize; y < sceneRect.bottom(); y += gridSize) {
        painter->drawLine(QLineF(sceneRect.left(), y, sceneRect.right(), y));
    }

    painter->save();
    painter->resetTransform();
    double crossSize = 10.0 / currentScale; // 中心十字标记的大小
    drawCenterMark(QPointF(0, 0), crossSize);
    painter->restore();
}

void CADView::mousePressEvent(QMouseEvent *event) {
    QPointF scenePos = mapToScene(event->pos());
    int gridSize = static_cast<int>(baseGridSize / transform().m11()); // 根据缩放动态计算网格大小
    if (gridSize < 5) {
        gridSize = 5;
    }
    // 将坐标转换为毫米
    qreal snapX = std::round(scenePos.x() / gridSize) * gridSize;
    qreal snapY = std::round(scenePos.y() / gridSize) * gridSize;
    QPointF snappedPos(snapX, snapY);
    QGraphicsView::mousePressEvent(event);
}

void CADView::drawLine(const QPointF &start, const QPointF &end) {
    qreal currentScale = transform().m11();
    qreal dynamicLineWidth = 2.0 / currentScale;  // 动态线宽
    QGraphicsLineItem *line = new QGraphicsLineItem(QLineF(start, end));
    line->setPen(QPen(Qt::blue, dynamicLineWidth));  // 使用动态线宽
    scene()->addItem(line);
}

void CADView::drawArc(const QPointF &center, double radius, double startAngle, double spanAngle) {
    qreal currentScale = transform().m11();
    qreal dynamicLineWidth = 2.0 / currentScale;  // 动态线宽
    QPainterPath path;
    path.arcTo(QRectF(center.x() - radius, center.y() - radius, 2 * radius, 2 * radius),
               startAngle, spanAngle);
    QGraphicsPathItem *arcItem = new QGraphicsPathItem(path);
    arcItem->setPen(QPen(Qt::green, dynamicLineWidth));  // 使用动态线宽
    scene()->addItem(arcItem);
}

void CADView::drawCircle(const QPointF &center, double radius) {
    qreal currentScale = transform().m11();
    qreal dynamicLineWidth = 2.0 / currentScale;  // 动态线宽
    QGraphicsEllipseItem *circle = new QGraphicsEllipseItem(
            center.x() - radius,
            center.y() - radius,
            2 * radius,
            2 * radius
    );

    circle->setPen(QPen(Qt::blue, dynamicLineWidth));     // 使用动态线宽
    circle->setBrush(QBrush(Qt::yellow));  // 填充颜色为黄色
    scene()->addItem(circle);              // 添加到场景中
    drawCenterMark(center, radius / 10);
}

void CADView::drawCenterMark(const QPointF &center, double crossSize) {
    if (horizontalLine != nullptr) {
        scene()->removeItem(horizontalLine);
        delete horizontalLine;
        horizontalLine = nullptr;
    }

    if (verticalLine != nullptr) {
        scene()->removeItem(verticalLine);
        delete verticalLine;
        verticalLine = nullptr;
    }

    qreal currentScale = transform().m11();
    qreal dynamicLineWidth = 2.0 / currentScale;  // 动态线宽
    QPen crossPen(Qt::red, dynamicLineWidth);  // 使用动态线宽

    verticalLine = new QGraphicsLineItem(
            center.x(), center.y() - crossSize,
            center.x(), center.y() + crossSize);
    verticalLine->setPen(crossPen);
    scene()->addItem(verticalLine);

    horizontalLine = new QGraphicsLineItem(
            center.x() - crossSize, center.y(),
            center.x() + crossSize, center.y());
    horizontalLine->setPen(crossPen);
    scene()->addItem(horizontalLine);
}

